local skynet = require "skynet"
local service_class = require "service_class"

local table_insert = table.insert
local table_unpack = table.unpack

local proxy_mgr = class("proxy_mgr",service_class)

function proxy_mgr:ctor() 
	self.data_proxys = {}
end

function proxy_mgr:reload() 
end

function proxy_mgr:start_proxy(proxy_num)
    for i = 1,proxy_num do
        local proxy = skynet.newservice('common_service','web_proxy')
        table_insert(self.data_proxys,proxy)
    end
end

function proxy_mgr:start(conf)
	self:start_proxy(conf.proxy_num)
    local proxy_index = 1
    skynet.dispatch('lua',function(_,_,...)
        
        print('now.... worker index is ',proxy_index)
        proxy_index = proxy_index % #self.data_proxys + 1
		local r = {skynet.call(self.data_proxys[proxy_index],'lua',...)}
		if #r > 0 then
			return skynet.retpack(table_unpack(r))
        end
        proxy_index = proxy_index + 1
	end)

	return true
end

return proxy_mgr